// index.js
// 圆盘获取应用实例
const app = getApp()

var canOnePointMove = false;

var pan1onePoint = { x: 0,y: 0};
var pan2onePoint = { x: 0,y: 0};
var pan3onePoint = { x: 0,y: 0};
var pan4onePoint = { x: 0,y: 0};

var pan1twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan2twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan3twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan4twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};


Page({
  data: {
    width: 0,
    height: 0,
    left: 375,
    top: 600,
    msg: '',
      src1: 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/693621231f0fe452e2a3dab3e265984b.png',
      scale1: 1,
      rotate1: 10,

      src2: 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/ae4f673cbe8a726e2f8c41fa5511ef87.png',
      scale2: 1,
      rotate2: 60,

      src3: 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/ee597656148cf9d628c01a635ab22421.png',
      scale3: 1,
      rotate3: 270,

      src4: 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d967bce4ec1b5d032f10d15be9079114.png',
      scale4: 1,
      rotate4: 130,

 },
 // 关闭上拉加载
  onReachBottom: function() {
 //return
 },
  bindload: function(e) {
 var that = this
 var width = e.detail.width
 var height = e.detail.height
 if (width > 750) {
      height = 750 * height / width
      width = 750
 }
 if (height > 1200) {
      width = 1200 * width / height
      height = 1200
 }
    that.setData({
      width: width,
      height: height
 })
 },

pantouchstart: function(e) {
 var that = this
 if (e.touches.length < 2) {
      canOnePointMove = true
      pan1onePoint.x = e.touches[0].pageX * 2
      pan1onePoint.y = e.touches[0].pageY * 2
 }else {
  pan1twoPoint.x1 = e.touches[0].pageX * 2
  pan1twoPoint.y1 = e.touches[0].pageY * 2
  pan1twoPoint.x2 = e.touches[1].pageX * 2
  pan1twoPoint.y2 = e.touches[1].pageY * 2
 }
 },

pantouchmove: function(e){
 var that = this
 if (e.touches.length < 2 && canOnePointMove) {
 var onePointDiffX = e.touches[0].pageX * 2 - pan1onePoint.x
 var onePointDiffY = e.touches[0].pageY * 2 - pan1onePoint.y
 var left = that.data.left + onePointDiffX;
 var top = that.data.top + onePointDiffY;
      that.setData({
        msg: '单点移动 left='+left+' top='+top,
        left: left,
        top: top,
      })
 pan1onePoint.x = e.touches[0].pageX * 2
 pan1onePoint.y = e.touches[0].pageY * 2
 }
 else if (e.touches.length > 1) {
 var preTwoPoint = JSON.parse(JSON.stringify(pan1twoPoint))
 pan1twoPoint.x1 = e.touches[0].pageX * 2
 pan1twoPoint.y1 = e.touches[0].pageY * 2
 pan1twoPoint.x2 = e.touches[1].pageX * 2
 pan1twoPoint.y2 = e.touches[1].pageY * 2
 // 计算角度，旋转(优先)
 var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
 var curAngle = Math.atan((pan1twoPoint.y1 - pan1twoPoint.y2)/(pan1twoPoint.x1 - pan1twoPoint.x2))*180/Math.PI
 if (Math.abs(perAngle - curAngle) > 1) {
      console.log('开始旋转....')
      var data1 = that.data.rotate1 + (curAngle - perAngle);
        that.setData({
          msg: '旋转 '+data1,
          rotate1: that.data.rotate1 + (curAngle - perAngle)
        })
        console.log('旋转....'+that.data.rotate1)
 }else {
 // 计算距离，缩放
 var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
 var curDistance = Math.sqrt(Math.pow((pan1twoPoint.x1 - pan1twoPoint.x2), 2) + Math.pow((pan1twoPoint.y1 - pan1twoPoint.y2), 2))
        that.setData({
          //msg: '缩放',
          //scale: that.data.q_pan1.scale + (curDistance - preDistance) * 0.005
 })
 }
 }
 },

 pantouchend: function(e) {
  var that = this
     canOnePointMove = false
  },

 pantouchmove2: function(e){
  var that = this
  if (e.touches.length < 2 && canOnePointMove) {
  var onePointDiffX = e.touches[0].pageX * 2 - pan2onePoint.x
  var onePointDiffY = e.touches[0].pageY * 2 - pan2onePoint.y
       that.setData({
         msg: '单点移动',
         //left: that.data.left + onePointDiffX,
         //top: that.data.top + onePointDiffY,
       })
  pan2onePoint.x = e.touches[0].pageX * 2
  pan2onePoint.y = e.touches[0].pageY * 2
  }else if (e.touches.length > 1) {
  var preTwoPoint = JSON.parse(JSON.stringify(pan2twoPoint))
  pan2twoPoint.x1 = e.touches[0].pageX * 2
  pan2twoPoint.y1 = e.touches[0].pageY * 2
  pan2twoPoint.x2 = e.touches[1].pageX * 2
  pan2twoPoint.y2 = e.touches[1].pageY * 2
  // 计算角度，旋转(优先)
  var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
  var curAngle = Math.atan((pan2twoPoint.y1 - pan2twoPoint.y2)/(pan2twoPoint.x1 - pan2twoPoint.x2))*180/Math.PI
  if (Math.abs(perAngle - curAngle) > 1) {
       console.log('开始旋转2....')
         that.setData({
           msg: '旋转2 '+that.data.rotate1 + (curAngle - perAngle),
           rotate2: that.data.rotate2 + (curAngle - perAngle)
         })
         console.log('旋转2....'+that.data.rotate2)
  }else {
  // 计算距离，缩放
  var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
  var curDistance = Math.sqrt(Math.pow((pan2twoPoint.x1 - pan2twoPoint.x2), 2) + Math.pow((pan2twoPoint.y1 - pan2twoPoint.y2), 2))
         that.setData({
           //msg: '缩放2',
           //scale: that.data.q_pan1.scale + (curDistance - preDistance) * 0.005
  })
  }
  }
  },
 
 pantouchstart2: function(e) {
  var that = this
  if (e.touches.length < 2) {
       //canOnePointMove = true
       pan2onePoint.x = e.touches[0].pageX * 2
       pan2onePoint.y = e.touches[0].pageY * 2
  }
  else {
   pan2twoPoint.x1 = e.touches[0].pageX * 2
   pan2twoPoint.y1 = e.touches[0].pageY * 2
   pan2twoPoint.x2 = e.touches[1].pageX * 2
   pan2twoPoint.y2 = e.touches[1].pageY * 2
  }
  },

  pantouchend2: function(e) {
  var that = this
     canOnePointMove = false
  },

  pantouchmove3: function(e){
    var that = this
    if (e.touches.length < 2 && canOnePointMove) {
    var onePointDiffX = e.touches[0].pageX * 2 - pan3onePoint.x
    var onePointDiffY = e.touches[0].pageY * 2 - pan3onePoint.y
         that.setData({
           msg: '单点移动',
           //left: that.data.left + onePointDiffX,
           //top: that.data.top + onePointDiffY,
         })
    pan3onePoint.x = e.touches[0].pageX * 2
    pan3onePoint.y = e.touches[0].pageY * 2
    }else if (e.touches.length > 1) {
    var preTwoPoint = JSON.parse(JSON.stringify(pan3twoPoint))
    pan3twoPoint.x1 = e.touches[0].pageX * 2
    pan3twoPoint.y1 = e.touches[0].pageY * 2
    pan3twoPoint.x2 = e.touches[1].pageX * 2
    pan3twoPoint.y2 = e.touches[1].pageY * 2
    // 计算角度，旋转(优先)
    var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
    var curAngle = Math.atan((pan3twoPoint.y1 - pan3twoPoint.y2)/(pan3twoPoint.x1 - pan3twoPoint.x2))*180/Math.PI
    if (Math.abs(perAngle - curAngle) > 1) {
         console.log('开始旋转3....')
           that.setData({
             msg: '旋转3 '+that.data.rotate1 + (curAngle - perAngle),
             rotate3: that.data.rotate3 + (curAngle - perAngle)
           })
           console.log('旋转3....'+that.data.rotate3)
    }else {
    // 计算距离，缩放
    var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
    var curDistance = Math.sqrt(Math.pow((pan3twoPoint.x1 - pan3twoPoint.x2), 2) + Math.pow((pan3twoPoint.y1 - pan3twoPoint.y2), 2))
           that.setData({
             //msg: '缩放',
             //scale: that.data.q_pan1.scale + (curDistance - preDistance) * 0.005
    })
    }
    }
    },
   
    pantouchstart3: function(e) {
    var that = this
    if (e.touches.length < 2) {
         //canOnePointMove = true
         pan3onePoint.x = e.touches[0].pageX * 2
         pan3onePoint.y = e.touches[0].pageY * 2
    }else {
     pan3twoPoint.x1 = e.touches[0].pageX * 2
     pan3twoPoint.y1 = e.touches[0].pageY * 2
     pan3twoPoint.x2 = e.touches[1].pageX * 2
     pan3twoPoint.y2 = e.touches[1].pageY * 2
    }
    },
  
    pantouchend3: function(e) {
    var that = this
       canOnePointMove = false
    },

    pantouchmove4: function(e){
      var that = this
      if (e.touches.length < 2 && canOnePointMove) {
      var onePointDiffX = e.touches[0].pageX * 2 - pan4onePoint.x
      var onePointDiffY = e.touches[0].pageY * 2 - pan4onePoint.y
           that.setData({
             msg: '单点移动4',
             //left: that.data.left + onePointDiffX,
             //top: that.data.top + onePointDiffY,
           })
      pan4onePoint.x = e.touches[0].pageX * 2
      pan4onePoint.y = e.touches[0].pageY * 2
      }else if (e.touches.length > 1) {
      var preTwoPoint = JSON.parse(JSON.stringify(pan4twoPoint))
      pan4twoPoint.x1 = e.touches[0].pageX * 2
      pan4twoPoint.y1 = e.touches[0].pageY * 2
      pan4twoPoint.x2 = e.touches[1].pageX * 2
      pan4twoPoint.y2 = e.touches[1].pageY * 2
      // 计算角度，旋转(优先)
      var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
      var curAngle = Math.atan((pan4twoPoint.y1 - pan4twoPoint.y2)/(pan4twoPoint.x1 - pan4twoPoint.x2))*180/Math.PI
      if (Math.abs(perAngle - curAngle) > 1) {
           console.log('开始旋转4....')
             that.setData({
               msg: '旋转4 '+that.data.rotate1 + (curAngle - perAngle),
               rotate4: that.data.rotate4 + (curAngle - perAngle)
             })
             console.log('旋转4....'+that.data.rotate4)
      }else {
      // 计算距离，缩放
      var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
      var curDistance = Math.sqrt(Math.pow((pan4twoPoint.x1 - pan4twoPoint.x2), 2) + Math.pow((pan4twoPoint.y1 - pan4twoPoint.y2), 2))
             that.setData({
               //msg: '缩放4',
               //scale: that.data.q_pan1.scale + (curDistance - preDistance) * 0.005
      })
      }
      }
      },
     
      pantouchstart4: function(e) {
      var that = this
      if (e.touches.length < 2) {
           //canOnePointMove = true
           pan4onePoint.x = e.touches[0].pageX * 2
           pan4onePoint.y = e.touches[0].pageY * 2
      }else {
       pan4twoPoint.x1 = e.touches[0].pageX * 2
       pan4twoPoint.y1 = e.touches[0].pageY * 2
       pan4twoPoint.x2 = e.touches[1].pageX * 2
       pan4twoPoint.y2 = e.touches[1].pageY * 2
      }
      },
    
      pantouchend4: function(e) {
      var that = this
         canOnePointMove = false
      }
  
})